package com.yydream.controller;

import com.yydream.common.aop.Secret;
import com.yydream.common.utils.AesUtil;
import com.yydream.common.utils.R;
import com.yydream.common.utils.RsaUtil;
import com.yydream.dto.SysUserDTO;
import com.yydream.vo.SysUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;


@RestController
@RequestMapping("/test")
public class TestController {

    @PostMapping("/test01")
    @Secret
    public R test01 (@RequestBody SysUserDTO sysUserDTO) {
        System.out.println("测试str" + sysUserDTO.toString());
        SysUserVo sysUserVo = new SysUserVo();
        BeanUtils.copyProperties(sysUserDTO, sysUserVo);
        sysUserVo.setAvatar("ceshi头像");
        return R.ok().put(sysUserVo);
    }
    @GetMapping("/test02")
    @Secret
    public R test02 (@RequestParam("id") String id, @RequestParam("name") String name) {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("id", id);
        stringStringHashMap.put("name", name);
        stringStringHashMap.put("test", "测试返回的get哦");
        return R.ok().put(stringStringHashMap);
    }

    @GetMapping("/getPublicKey")
    public R test02 () {
        return R.ok().put(RsaUtil.getPublicKey().toString());
    }

//    public static void main(String[] args) throws Exception {
        /*SysUserDTO sysUserDTO = new SysUserDTO();
        sysUserDTO.setPhone("1008611");
        sysUserDTO.setUsername("yydream123456");
        String jsonString = JSON.toJSONString(sysUserDTO);
        String encrypt = AESUtils.encrypt(jsonString);
        System.out.println(encrypt);

        String decrypt = AESUtils.decrypt("41A7D8C15C5701B63DCD92D4FC230B7CFFD9F4ED021454CBD544E4224EF82FCFF6C0999DDD95D2ACA0EADB29E66E30BDDC79088D867ED9575D9AA84F37FC796B44B217E4195E02D50E7AC8107C2B2D11");
        Object parse = JSON.parse(decrypt);
        System.out.println(parse.toString());*/

//        String publicKey = RsaUtil.getPublicKey();
//        String key = "1B6A6C4BC15AA3B1";
//        String s = RsaUtil.encryptByPublicKey(publicKey, key);
//
//        SysUserDTO sysUserDTO = new SysUserDTO();
//        sysUserDTO.setPhone("1008611");
//        sysUserDTO.setUsername("yydream123456");
//        String jsonString = JSON.toJSONString(sysUserDTO);
//        String encrypt = AesUtil.encryptAES(jsonString, key);
//
//        String privateKey = RsaUtil.getPrivateKey();
//
//        String key2 = RsaUtil.decryptByPrivateKey(privateKey, s);
//        String s1 = AesUtil.decryptAES(encrypt, key2);
//        System.out.println(s1);

//        String decryptData  = "{\"username\":\"412\",\"password\":\"123\"}";
//        String key = "C1EFE5ED5DE95F5A";
//        String deData = AesUtil.encryptAES(decryptData, key);
//
//        System.out.println(deData);
//        String s = AesUtil.decryptAES(deData, key);
//        System.out.println(s);

//    }
public static void main(String[] args) {
    try {
        String key = "208F4A3935A0EA71"; // 16字节密钥

        // 前端传来的Base64编码的密文
//        String encryptedBase64 = "mnL2Mcq8tkIUOSF5IS7XXR88Z0ySEgCP6qyo70OBrBPKMc+f0iQ3ix4XLa/LJLN3";
//        String encryptedBase64 = "9a72f631cabcb64214392179212ed75d1f3c674c9212008feaaca8ef4381ac13ca31cf9fd224378b1e172dafcb24b377";
        String encryptedBase64 = "OUE3MkY2MzFDQUJDQjY0MjE0MzkyMTc5MjEyRUQ3NUQxRjNDNjc0QzkyMTIwMDhGRUFBQ0E4RUY0MzgxQUMxM0NBMzFDRjlGRDIyNDM3OEIxRTE3MkRBRkNCMjRCMzc3";

        // 解码
        byte[] encrypted = Base64.getDecoder().decode(encryptedBase64);

        System.out.println("Decoded byte array length: " + encrypted.length);
        String s = new String(encrypted);
        System.out.println(s);
        byte[] bytes = AesUtil.hexStringToBytes(s);
        // 初始化AES解密
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        IvParameterSpec ivParams = new IvParameterSpec(key.getBytes(StandardCharsets.UTF_8));

        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParams);
        byte[] decryptedBytes = cipher.doFinal(bytes);
        String decrypted = new String(decryptedBytes, "UTF-8");


        System.out.println("Decrypted: " + decrypted);
    } catch (Exception e) {
        e.printStackTrace(); // 输出详细的错误信息
    }
//        String decryptData  = "{\"username\":\"412\",\"password\":\"123\"}";
//        String key = "C1EFE5ED5DE95F5A";
//        String deData = AesUtil.encryptAES(decryptData, key);
//
//        System.out.println(deData);
//        String s = AesUtil.decryptAES(deData, key);
//        System.out.println(s);
}
}
